package cn.liujianbo.example.base.lambda;

import lombok.SneakyThrows;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 刘建波
 * @date 2022-03-06 20:10
 */
public class LambdaTest {
    @SneakyThrows
    public static void main(String[] args) {
//        Stream.of(1,2,3,4,5).forEach(System.out::print);
//        System.out.println("=========================");
//        Stream.iterate(0,x->x+3).limit(5).map(x ->x + ",").forEach(System.out::print);
//        System.out.println("=========================");
//        Stream.generate(Math::random).limit(5).map(x -> x + ",").forEach(System.out::print);
//        System.out.println("=========================");
//        System.out.println(Stream.iterate(0, x -> x + 3).limit(100000).filter(x -> x > 1000).findFirst().get());
//        System.out.println(Stream.iterate(0, x -> x + 3).limit(100000).parallel().filter(x -> x > 1000).findAny().get());
//        System.out.println(Stream.iterate(0, x -> x + 3).limit(100000).anyMatch(x->x==1002));
//        System.out.println("=========================");
//        List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);
//        System.out.println(list.stream().max(Integer::compareTo).get());
//        System.out.println(list.stream().max((a,b)-> a>b?-1:1).get());
//        List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
//        List<String> collect = list.stream().flatMap(x -> Stream.of(x.split(","))).collect(Collectors.toList());
//        collect.forEach(System.out::print);

//        List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);
//        IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(e -> e));
//        System.out.println("collect.getAverage() = " + collect.getAverage());
//        System.out.println("collect.getMax() = " + collect.getMax());
//        System.out.println("collect.getMin() = " + collect.getMin());
//        System.out.println("collect.getSum() = " + collect.getSum());
//        System.out.println("collect.getCount() = " + collect.getCount());
//        Map<Boolean, List<Integer>> map = list.stream().collect(Collectors.partitioningBy(x -> x > 6));
//        map.get(true).forEach(System.out::print);
//        System.out.println("=======================");
//        map.get(false).forEach(System.out::print);
//        System.out.println("=======================");
//        System.out.println(list.stream().map(x->x.toString()).collect(Collectors.joining("_")));
//
//        String[] arr1 = { "a", "b", "c", "d" };
//        String[] arr2 = { "d", "e", "f", "g" };
//
//        Stream<String> stream1 = Stream.of(arr1);
//        Stream<String> stream2 = Stream.of(arr2);
//        // concat:合并两个流 distinct：去重
//        List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
//        // limit：限制从流中获得前n个数据
//        List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
//        // skip：跳过前n个数据
//        List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());
//
//        System.out.println("流合并：" + newList);
//        System.out.println("limit：" + collect);
//        System.out.println("skip：" + collect2);
//
//        Optional<Object> o = Optional.ofNullable(null);

        String s = new String(Files.readAllBytes(Paths.get("C:\\dev\\IdeaProjects\\studyDemo\\base\\src\\main\\java\\cn\\liujianbo\\example\\base\\lambda\\abc.txt")));
        String[] arr = s.split("\\PL+");
        List<String> list = Arrays.asList(arr);
        list.forEach(System.out::println);

        long count = list.stream().filter(e -> e.length() > 3).count();
        System.out.println("count = " + count);

        Consumer<Object> consumerLambda = System.out::println;

        Stream.of(arr).forEach(consumerLambda);

        Arrays.stream(arr).forEach(consumerLambda);
        Arrays.stream(arr, 0,1).forEach(consumerLambda);

        Stream.generate(()->"a").limit(5).forEach(consumerLambda);

        Stream.generate(Math::random).limit(2).forEach(consumerLambda);

        Stream.iterate(0, n -> ++n).limit(5).forEach(consumerLambda);

        Stream<String> stream = Files.lines(Paths.get("C:\\dev\\IdeaProjects\\studyDemo\\base\\src\\main\\java\\cn\\liujianbo\\example\\base\\lambda\\abc.txt"));
        stream.forEach(consumerLambda);

        Stream<String> stream1 = Arrays.stream(arr);
        arr[0] = "aaa";
        stream1.forEach(consumerLambda);

        Map<Integer, Long> collect = Arrays.stream(arr).collect(Collectors.groupingBy(String::length, Collectors.counting()));
        collect.forEach((k,v)-> System.out.println("k = " + k + ",v = " + v));

        Map<Integer, Integer> collect1 = Arrays.stream(arr).collect(Collectors.groupingBy(String::length, Collectors.collectingAndThen(Collectors.toList(), List::size)));
        collect1.forEach((k,v)-> System.out.println("k = " + k + ",v = " + v));
    }
}
